package com.aca.market.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@Slf4j
public class ShiroConfig implements ApplicationContextAware, InitializingBean {

    private static ApplicationContext context;


    //其余的shiro相关bean放在beanRegisty里提前初始化 因为shiro依赖的业务bean会初始化
    //不然not eligible for auto-proxying：如果有业务bean先于任何BPP实例化，
    // 那么这个业务bean就不会被还未实例化的BPP处理
    @Bean
    public ShiroDialect shiroDialect(){
        log.info("shiroDialect init");
        return new ShiroDialect();
    }

//    @Bean
//    public FilterRegistrationBean getFilterRegistration() {
//        //创建一个过滤器注册处理对象
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        //创建一个代理过滤器设置给该注册对象
//        filterRegistrationBean.setFilter(new DelegatingFilterProxy());
//        //指定过滤器的名称【注意事项，必须和下面配置代理的Bean的名通过】
//        filterRegistrationBean.setName("shiroFilterFactoryBean");
//        //设置参数
//        //filterRegistrationBean.addInitParameter("targetFilterLifecycle", "true");
//        //设置拦截路径
//        filterRegistrationBean.addUrlPatterns("/*");
//        return filterRegistrationBean;
//    }

    @Bean
    public CookieRememberMeManager cookieRememberMeManager(){
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        //cookie必须设置name
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        cookie.setMaxAge(30*24*60*60);

        rememberMeManager.setCookie(cookie);
        return  rememberMeManager;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
//        context.getBean("userRealm");
//        context.getBean("securityManager");
//        context.getBean("shiroFilterService");
        log.info("&&&&&&&&&&&&&&&&&&&&&&&&&&&setApplicationContext Over");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("afterPropertiesSet===========reloadFilterChains");
        //没有在shiroFilterFactory初始化filterMap 用service会空指针
        // afterPropertiesSet后把过滤策略加入
        //这样热部署会有问题 放在filterListener了
//        IShiroFilterService shiroFilterService = (IShiroFilterService) context.getBean("shiroFilterService");
//        shiroFilterService.reloadFilterChains();
    }


//    @Bean
//    public UserRealm userRealm(){
//        return new UserRealm();
//    }

//    @Bean(name = "securityManager")
//    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
//        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
//        defaultWebSecurityManager.setRealm(userRealm);
//        return defaultWebSecurityManager;
//    }

//    @Lazy
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
//        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//        //设置安全管理器
//        bean.setSecurityManager(securityManager);
//        //添加内置过滤器:
//        //anon:无需认证就可以访间
//        //authc:必须认证了才能让访问
//        //user:必须拥有记住我功能才能用
//        //perms:拥有对某 个资源的权限才能访间
//        //role:拥有某 个角色权限才能访间
//
//        Map<String,String> filterMap = new LinkedHashMap<>();
//        filterMap.put("/img/**","anon");
//        filterMap.put("/css/**","anon");
//        filterMap.put("/js/**","anon");
////        filterMap.put("/shop.html","anon");
//        filterMap.put("/login","anon");
//        filterMap.put("/shop/items/*","anon");
//        filterMap.put("/shop/**","anon");
//        filterMap.put("/pc","anon");
//        filterMap.put("/product-single.html","anon");
//        filterMap.put("/success","authc");
//        filterMap.put("/failure","authc");
////        filterMap.put("/addcar","perms[user:add]");
////        filterMap.put("/updatecar/*","perms[user:update]");
////        filterMap.put("/delcar/*","perms[user:del]");
//
//        bean.setFilterChainDefinitionMap(filterMap);
//
//        //无权限就去登陆
//        bean.setLoginUrl("/login");
//        //未授权情况
//        bean.setUnauthorizedUrl("/no-auth");
//        return bean;
//    }

}
